import { getWorkcard } from './storage'

function getAllAttachments(workcard) {
  const attachments = []

  workcard.workinstructions.forEach(
    workinstruction => {
      workinstruction.attachments.forEach(
        attachment => {
          if (attachment.uid) {
            attachments.push(attachment)
          }
        }
      )
    }
  )
  if (workcard.gygf && workcard.gygf.uid) {
    attachments.push(workcard.gygf)
  }

  return attachments
}

function getAllPhotoObjects(workcard) {
  const photos = []

  workcard.workinstructions.forEach(
    workinstruction => {
      workinstruction.executeresult.forEach(
        exeresult => {
          exeresult.photos.forEach(
            photo => {
              photos.push(photo)
            }
          )
        }
      )
    }
  )

  return photos
}

function getWorkcardCompleteStatistic(workcard) {
  const result = {
    work: '', 
    check: '', 
    recheck: '' 
  }

  const total = workcard.workinstructions.length
  const work = workcard.workinstructions.filter(
    workinstruction => workinstruction.executeresult.find(v => v.role === '工作者' && v.executestatus === '已执行')
  ).length
  const check = workcard.workinstructions.filter(
    workinstruction => workinstruction.executeresult.find(v => v.role === '检验' && v.executestatus === '已执行')
  ).length
  const recheck = workcard.workinstructions.filter(
    workinstruction => workinstruction.executeresult.find(v => v.role === '复检' && v.executestatus === '已执行')
  ).length

  result.work = work + '/' + total
  result.check = check + '/' + total
  result.recheck = recheck + '/' + total

  return result
}

function mergeExistWorkcard(newWorkcard) {
  return getWorkcard(newWorkcard.workuid).then(
    workcard => {
      if (!workcard) {
        return [newWorkcard, getAllPhotoObjects(newWorkcard), [], getAllAttachments(newWorkcard)]
      }

      let newPhotos = []
      let deletePhotos = []
      workcard.workcard.workinstructions.forEach(
        workinstruction => {
          const newWorkinstruction = newWorkcard.workinstructions.find(v => v.uid === workinstruction.uid)
          if (newWorkinstruction) {
            if (newWorkinstruction.parts.length !== 0) {
              workinstruction.parts = newWorkinstruction.parts
            }
            newWorkinstruction.executeresult.forEach(
              s => { 
                const sameRole = workinstruction.executeresult.find(w => w.role === s.role)
                if (sameRole) { 
                  sameRole.name = s.name
                  sameRole.decision = s.decision
                  sameRole.date = s.date
                  sameRole.comment = s.comment
                  sameRole.executestatus = s.executestatus
                  sameRole.workhour = s.workhour
                  deletePhotos = deletePhotos.concat(sameRole.photos)
                  newPhotos = newPhotos.concat(s.photos)
                  sameRole.photos = s.photos
                } else {
                  newPhotos = newPhotos.concat(s.photos)
                  workinstruction.executeresult.push(s)
                }
              }
            )
          }
        }
      )
      return [workcard.workcard, newPhotos, deletePhotos, []]
    }
  )
}

function getWorkcardID(workcard) {
  return workcard.workrelattrs.find(v => v.name === 'ID').value
}

export { getAllAttachments, getAllPhotoObjects, getWorkcardCompleteStatistic, mergeExistWorkcard, getWorkcardID }
